package com.telnetnz.ticketdesk.dao.wrapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;

import com.rockbb.common.dao.CacheManagerBean;
import com.telnetnz.ticketdesk.dao.entity.ProjectEntity;
import com.telnetnz.ticketdesk.dao.mapper.ProjectMapper;

public class ProjectWrap {

	public static final String[] ORDER_BY = { 
		"project_name"
	};

	public static final String[] ORDER = {"DESC", "ASC"};

	public static final String[] STATUSES = {"normal", "inactivated"};

	private ProjectMapper mapper;
	private Cache cache;

	public ProjectWrap() {}

	public ProjectWrap(
			ProjectMapper mapper, 
			CacheManagerBean cacheManagerBean) {
		this.mapper = mapper;
		this.cache = cacheManagerBean.get(mapper.getClass().getName());
	}

	public ProjectEntity select(int id) {
		return mapper.select(id);
	}

	public long countAll() {
		return mapper.countAll();
	}

	public void insert(ProjectEntity entity) {
		mapper.insert(entity);
	}

	public void update(ProjectEntity entity) {
		mapper.update(entity);
	}

	public void delete(ProjectEntity entity){
		mapper.delete(entity);
	}

	public void deleteByPrimaryKey(int id) {
		mapper.deleteByPrimaryKey(id);
	}

	public List<Integer> selectByPageAndCondition(
			int start,
			int limit, 
			int orderby, 
			int order, 
			List<Integer> excludedIds)
	{
		Map<String, Object> parameters = new HashMap<String, Object>();

		if (start < 0) {parameters.put("start", 0);}
		else {parameters.put("start", start);}

		if (limit <=0) {parameters.put("limit", 20);}
		else {parameters.put("limit", limit);}

		if (orderby > ORDER_BY.length - 1 || orderby < 0) {parameters.put("orderby", ORDER_BY[0]);}
		else {parameters.put("orderby", ORDER_BY[orderby]);}

		parameters.put("order", (order == 0) ? ORDER[0] : ORDER[1]);

		if (excludedIds != null && excludedIds.size() > 0) {parameters.put("excluded_ids", excludedIds);}

		return mapper.selectByPageAndCondition(parameters);
	}

	public int countByCondition(List<Integer> excludedIds)
	{
		Map<String, Object> parameters = new HashMap<String, Object>();
		if (excludedIds != null && excludedIds.size() > 0) {parameters.put("excluded_ids", excludedIds);}

		return mapper.countByCondition(parameters);
	}

	public Cache getCache() 
	{
		return cache;
	}
}

